package com.minglead.crawler.schedule;

import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.io.IOException;
import java.util.Date;

public abstract class ScheduledTaskCommon implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(() -> {
            try {
                execute();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }, triggerContext -> {
            CronTrigger cronTrigger = new CronTrigger(getCron());
            Date nextExecDate = cronTrigger.nextExecutionTime(triggerContext);
            return nextExecDate;
        });
    }

    //频率
    public abstract String getCron();

    //执行定时任务的具体逻辑
    public abstract void execute() throws IOException;
}
